package com.zds.hj.poseidon.biz.dispatcher.credit;

import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.InstructTypeEnum;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.*;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author duanhao
 * @Description 授信选择产品测试模块
 * @Date 2023/5/25 9:40
 */

@Slf4j
@DispatcherHandler(value = "CreditDemoHandler", desc = "授信选择产品测试模块",applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
public class CreditDemoHandler extends CreditCheckAbsHandler {

    private static final String RESULT_URL = CreditDemoHandler.class.getName() + ".RESULT_URL";

    private static final String CHOICE_ROUTE = CreditDemoHandler.class.getName() + ".CHOICE_ROUTE";
    private static final String IMG_CODE = CreditDemoHandler.class.getName() + ".IMG_CODE";

    @Resource
    private CreditApplyService creditApplyService;



    @Override
    public Boolean skipThisStep(BaseReqModel params) {
        // TODO: 2023/5/26 进入模块首先判断跳过逻辑

        // true 为跳过本模块，执行下个模块
        // false 为不跳过


        return false;
    }

    @Override
    public BaseResultModel doSection(BaseReqModel baseReqModel) {




        return null;
    }

    @Override
    public Object preHanding(BaseReqModel params) {
        // TODO: 2023/5/26 发送短信，填充协议信息，初始化订单状态
        return null;
    }

    @Override
    public Object redoPreHanding(BaseReqModel params) {
        // TODO: 2023/5/26 重新发送短信

        CreditInfoEntityDto credit = creditApplyService.findCreditOnTransit(params);
        return credit;
    }



    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {
        log.info("模块执行逻辑");

        dbData.setName("张三");

    }

    @Override
    public Object echoDetail(BaseReqModel params) {

        List<InstructDto> instructSettingDtoList = params.getDispatchModuleNodes().getInstructSettingDtos();
        Map<String, String> key = instructSettingDtoList.stream().collect(Collectors.toMap(InstructDto::getInstructCode, InstructDto::getValues));
        String choiceRoute = key.get(CHOICE_ROUTE);
        log.info("测试模块执行逻辑,单行文本参数: {}", choiceRoute);
        String resultUrl = key.get(RESULT_URL);
        log.info("测试模块执行逻辑,下拉单选文本参数: {}", resultUrl);
        String imgCode = key.get(IMG_CODE);
        log.info("测试模块执行逻辑,图片参数: {}", imgCode);

        Map<String, Object> map = new HashMap<>();

        map.put("RESULT_URL", resultUrl);
        map.put("IMG_CODE", imgCode);
        map.put("CHOICE_ROUTE", choiceRoute);

        return map;
    }

    /**
     * 模块指令
     * @return
     */
    @Override
    public List<InstructRegisterModule> moduleInstructs() {
        List<InstructRegisterModule> instructRegisterModules = new ArrayList<>();
        InstructRegisterModule registerModule0 = new InstructRegisterModule();
        registerModule0.setInstructCode(RESULT_URL);
        registerModule0.setInstructName("测试模块参数");
        registerModule0.setInstructTitle("测试模块参数");
        registerModule0.setInstructType(InstructTypeEnum.ONE_LINE_TEXT);
        registerModule0.setInstructEmphatic("测试模块参数");
        registerModule0.setEmptyCheck(true);
        instructRegisterModules.add(registerModule0);


        InstructRegisterModule registerModule2 = new InstructRegisterModule();
        registerModule2.setInstructCode(IMG_CODE);
        registerModule2.setInstructName("产品介绍图片");
        registerModule2.setInstructTitle("产品介绍图片");
        registerModule2.setInstructType(InstructTypeEnum.UPLOAD);
        registerModule2.setInstructEmphatic("产品介绍图片");
        registerModule2.setEmptyCheck(true);
        instructRegisterModules.add(registerModule2);

        InstructRegisterModule registerModule1 = new InstructRegisterModule();
        registerModule1.setInstructCode(CHOICE_ROUTE);
        registerModule1.setInstructName("下拉单选测试");
        registerModule1.setInstructTitle("下拉单选测试");
        registerModule1.setInstructType(InstructTypeEnum.PULL_DOWN_ONE);
        registerModule1.setInstructEmphatic("下拉单选测试");
        registerModule1.setValueSource(1);  // 值的来源 自定义值
        registerModule1.setInstructValueByCustom("1@随机^2@循环^3@自定义");
        registerModule1.setEmptyCheck(true);
        instructRegisterModules.add(registerModule1);


        return instructRegisterModules;
    }

    @Override
    public ModuleBaseInfo moduleBaseInfo() {
        ModuleBaseInfo info = super.moduleBaseInfo();
        info.setAuto(false);
        return info;
    }

}
